<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
use App\Model\Backstage\Achievement;
use Illuminate\Http\Request;

/**
 * Class RoomWordbook
 * @package App\Model\Backstage
 * 楼盘字典model
 */
class RoomWordbook extends Base
{
    //楼盘字段数据表
    protected  $table;
    public function __construct()
    {
        parent::__construct();
       // $this->table = 'room_wordbook_'.$this->branch_id;  //后期分表
        $this->table = 'room_wordbook';
    }

    //地铁数组
    public static $metro_arr = ['1号线','2号线','3号线','4号线'];

    //环线数据
    public static $ring_arr = ['1环内', '1环-2环', '2环-3环', '3环-4环','4环-5环','5环-6环','6环外'];

    //结构
    public static $structure_arr = ['框架', '框剪', '砖混', '钢构'];

    //土地
    public static $land_arr = [ '一级',  '二级', '三级', '四级',  '五级', '六级', '七级', '八级'];

    //外墙
    public static $wall_arr = ['墙砖', '瓷砖',  '涂料', '砂浆', '石米'];

    /**
     * @param $param
     * @return mixed
     * 添加楼盘子典
     */
    public function insertWordbook($param){
        $id = DB::table($this->table)->where('w_name', $param['w_name'])->value('w_id');
        if ($id) {
            return -1;
        }
        $param['w_created_at'] = time();
        $param['w_updated_at'] = time();
        return DB::table($this->table)->insert($param);
    }

    /**
     * @param $id
     * @return mixed
     * 删除楼盘子典 此处做软删除
     */
    public function delWordbook($id){
       return DB::table($this->table)->where(['w_id' => $id])->update([
           'w_is_del'     => 0,
           'w_updated_at' => time()
       ]);
    }

    /**
     * @param $param
     * @return mixed
     * 修改楼盘字典
     */
    public function saveWordbook($param){
        $param['w_updated_at'] = time();
        return DB::table($this->table)->where(['w_id' => $param['w_id']])->update($param);
    }

    /**
     * @param $pageLimit
     * @param array $where
     * @param $keywords
     * @return mixed
     * 查询楼盘字典列表数据
     */
    public function selectWordbook($pageLimit, $where = [], $keywords){

        $list = DB::table($this->table)
            ->where(function ($query) use($where, $keywords){
                $query->where($where)->where('w_name', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords) {
                $query->where($where)->where('w_spell', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords){
                $query->where($where)->where('w_owner', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords){
                $query->where($where)->where('w_owner_phone', 'like', '%'.$keywords.'%');
            })
            ->orderBy('w_id', 'desc')
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    /**
     * @param $map
     * @return mixed
     * 获取添加房源信息时楼盘字典数据
     */
    public function getRoomWordbook($where, $keywords){
        return  DB::table($this->table)
            ->where(function ($query) use($where, $keywords){
                $query->where($where)->where('w_spell', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($where, $keywords) {
                $query->where($where)->where('w_name', 'like', '%'.$keywords.'%');
            })
            ->select('w_id', 'w_name')
            ->orderBy('w_id', 'desc')
            ->get();
    }
    //添加房源的时候用到的方法根据楼盘字典名称获取楼盘ID
    public function getRoomId($wName)
    {
        return DB::table($this->table)->where('w_name', $wName)->value('w_id');
    }

    /**
     * @param $id
     * @return mixed
     * 获取楼盘字典信息
     */
    public function getWordbook($id){
        return DB::table($this->table)
            ->where('w_id',$id)
            ->value('w_name');
    }





}